﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Orderconsole
{
  public  class OrderService
    {
        protected long OrderNumber;
        //建立订单列表
        List<Order> OrderList = new List<Order>();
        // 显示订单列表
        public void OrderDisplay()
        {
            foreach (Order test in OrderList)
            {
                Console.WriteLine(test.ToString());
            }
        }
        //增加订单
        public void Addorder(Order order)
        {
            bool tag = true;
            //判断是否有重复订单
            foreach (Order test in this.OrderList)
            {
                if (test.Equals(order))
                {
                    tag = false;
                }
            }
            if (tag)
            {
                this.OrderList.Add(order);
            }
        }
        // 删除订单
        public void Deleteorder(int number)
        {
            bool tag = false;
            foreach (Order test in this.OrderList)//判断已有订单列表里是否有要删除的订单
            {
                if (test.OrderNumber == number)
                {
                    tag = true;
                    this.OrderList.Remove(test);
                }
            }
            if (!tag)
            {
                Console.WriteLine(" 未找到该订单，请重新核对");
            }

        }
        //修改订单
        public void AlterOrder()
        {

            foreach (Order o in this.OrderList)
            {
                Console.WriteLine("请选择你要修改的属性：1.订单编码 2.商品名称 3用户名称  ");
                int oo = Convert.ToInt32(Console.ReadLine());
                switch (oo)
                {
                    case 1:
                        o.OrderNumber = Convert.ToInt32(Console.ReadLine());
                        break;
                    case 2:
                        o.ProductName = Convert.ToString(Console.ReadLine());
                        break;
                    case 3:
                        o.CustomerName = Console.ReadLine();
                        break;

                    default:
                        Console.WriteLine("修改失败！");
                        break;
                }

            }
        }
        //查询订单 按照用户名
        public void SearchOrder(string s)
        {
            bool tag = false;
            foreach (Order o in this.OrderList)
            {
                if (o.CustomerName.Equals(s))
                {
                    tag = true;
                    Console.WriteLine(o.ToString());
                }

            }
            if (!tag)
            {
                Console.WriteLine("未找到该订单！");
            }


        }
        //查询按照 订单编码
        public void SearchOrder(int i)
        {
            bool tag = false;
            foreach (Order o in this.OrderList)
            {
                if (o.OrderNumber.Equals(o))
                {
                    tag = true;
                    Console.WriteLine(o.ToString());
                }

            }
            if (!tag)
            {
                Console.WriteLine("未找到该订单！");
            }
        }
        //订单排序
        public void SortOrder()
        {
            var query = from order
                        in this.OrderList
                        orderby order.OrderAmount
                        select order;
            this.OrderList = query.ToList<Order>();
        }
        //将订单序列化为xml格式
        public void Export(String Name)
        {
            XmlSerializer ee = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(Name, FileMode.Create))
            {
                ee.Serialize(fs, OrderList);
            }
        }
        //从XML文件中载入订单
        public void Import(string path)
        {
            XmlSerializer ee = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                List<Order> temp = (List<Order>)ee.Deserialize(fs);
                temp.ForEach(order => {
                    if (!OrderList.Contains(order))
                    {
                        OrderList.Add(order);
                    }
                });
            }
        }

        /*    Array.ForEach(orders, p => Console.WriteLine(p));
            XmlSerializer xmlserializer = new XmlSerializer(typeof(Order[]));
            using (FileStream id = new FileStream("i.xml", FileMode.Create)) {
                xmlserializer.Serialize(id, orders);
            }
            Console.WriteLine("\nserialized as xml");
            Console.WriteLine(File.ReadAllText("i.xml", FileMode.Open)){
                Order[] orders3 = (Order[])xmlserializer.Deserialize(id);
                Console.WriteLine("\n Deserialized from i.xml");
                Array.ForEach(orders3, o => Console.WriteLine(o));*/

    }
    }
